package com.zjbbkj.baibai.netty.message.receive;

import com.zjbbkj.baibai.common.utils.wanli.DateUtils;
import com.zjbbkj.baibai.common.utils.wanli.RedisCacheUtil;
import com.zjbbkj.baibai.netty.maps.ContainerMaps;
import com.zjbbkj.baibai.netty.message.factory.Operation;
import com.zjbbkj.baibai.netty.pojo.ProtocolMessage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Map;
import static com.zjbbkj.baibai.netty.message.factory.OperatorFactory.write;
import static com.zjbbkj.baibai.netty.message.sendpackage.ADV.ADV_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.AID.AID_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.PING.PG_DEVICE;
import static com.zjbbkj.baibai.netty.message.sendpackage.STB.STB_DEVICE;

/**
 * @Description 处理PING消息
 * @author WanLi
 * @date 2021年08月24日 16:45
 */
@Slf4j
@Component
public class ReceivePing implements Operation {

    @Resource
    private RedisCacheUtil redisCacheUtil;

    @Override
    public void parsingMsg(String message, Channel channel) {
        try {
            Map<String, Channel> channels = ContainerMaps.getChannels();
            Map<Channel, String> channelsX = ContainerMaps.getChannelsX();
            //回复心跳包确认信息
            write(PG_DEVICE(), channel);
            if (channelsX.containsKey(channel)) {
                String IMEIID = channelsX.get(channel);
                if (!channels.containsKey(IMEIID)) {
                    ContainerMaps.removeGatewayChannelX(channel);
                    ContainerMaps.removeGatewayChannel(IMEIID);
                    write(ADV_DEVICE(), channel);
                } else {
                    if(redisCacheUtil.hasKey("PING_ON_OFF")){
                        String ping_on_off = redisCacheUtil.get("PING_ON_OFF").toString();
                        // System.out.println(ping_on_off);
                        if(ping_on_off.equals("1")){
                            log.info("收到" + IMEIID + "的信息" + message);
                        }
                    }
                    //存入redis缓存
                    savePing(message, IMEIID);
                }
            } else {
                write(ADV_DEVICE(), channel);
                write(AID_DEVICE(), channel);
                write(STB_DEVICE("ALL"), channel);
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

    public void savePing(String cmd, String IMEIID) {
        //解析PING消息

        String[] split = cmd.split(",");
        String signal = split[0].substring(split[0].length() - 2);//信号值
        String[] parameters = split[1].split("#/#");
        String errorRate = parameters[0]; //误码率
        String resSpeed = parameters[1];  //响应速度
        String networkType = parameters[2]; //网络制式
        //log.info(signal + "----" + errorRate + "-----" + resSpeed + "-----" + networkType);
        //保存PING信息
        redisCacheUtil.set(IMEIID + "_PING", signal, 180);
        ProtocolMessage p = new ProtocolMessage();
        if (redisCacheUtil.hasKey(IMEIID)) {
            p = (ProtocolMessage) redisCacheUtil.hmget(IMEIID);
        }
        p.setIMEIID(IMEIID);
        p.setSignal(signal);
        p.setLastContactTime(DateUtils.getStringDate());
        p.setDeviceStatus("1");
        p.setErrorRate(errorRate);
        p.setResSpeed(resSpeed);
        p.setNetworkType(networkType);

        redisCacheUtil.set(IMEIID,p);
    }
}
